package com.gper.edu.vip;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ellis.guan
 * @Description:
 * @Date: 2020/3/21 13:10
 * @Version: 1.0
 */
public class VideoConvertM4sToMp4 {

    private static String command = "D:\\developer\\app\\ffmpeg\\bin\\ffmpeg.exe -i %s -i %s -c:v copy -c:a copy d:/video/docker/Docker核心技术-%s.mp4";
    private static final String path="D:\\download\\27122140";
    private static Integer count=1;

    public static void convertM4sToMp4(File audio, File video){
        Runtime runTime = Runtime.getRuntime();
        if (runTime == null) {
            throw new RuntimeException("Create runtime false!");
        }

        Process process = null;
        try {
            String parentPath = audio.getParentFile().getAbsolutePath();
            String name = parentPath.replace(path,"").replace(File.separator,"-").split("-")[1];
            if (name.length() ==1 ){
                name = "00"+name;
            }else if (name.length() == 2){
                name="0"+name;
            }
            String executionCommand = String.format(command, audio.getAbsolutePath(),video.getAbsolutePath(),name);
            System.out.println(executionCommand);
            process = runTime.exec(executionCommand);
            Thread.sleep(100);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void getConvertFiles(List<Map<String,File>> convertFiles, File directory){
        if (directory.isFile()) return;
        File[] files = directory.listFiles();
        Map<String,File> fileMap = null;
        for (File file : files){
            if (fileMap == null) fileMap = new HashMap<>();
            if (file.getName().endsWith(".m4s")){
                fileMap.put(file.getName(),file);
            }
            if (file.isDirectory()) getConvertFiles(convertFiles,file);
        }
        if (fileMap != null || fileMap.size() != 0) convertFiles.add(fileMap);
    }

    public static void renameFile(){
        String filePath = "G:\\Flink\\尚硅谷\\Flink实践";
        File directory = new File(filePath);
        File[] files = directory.listFiles();
        String fileName="";
        for (File file:files){
            fileName = file.getName();
            if (!fileName.startsWith("Flink-")) continue;
            file.renameTo(new File(file.getParentFile().getAbsolutePath(),fileName.replace("Flink-","0").replace("64","Flink")));
        }
    }
    public static void main(String[] args) {
//        renameFile();

        System.out.println("dd");
        List<Map<String,File>> convertFiles = new ArrayList<>();
        getConvertFiles(convertFiles,new File(path));
        System.out.println(JSON.toJSON(convertFiles));
        for (Map<String,File> fileMap : convertFiles){
            if (fileMap.size() == 0) continue;
            convertM4sToMp4(fileMap.get("audio.m4s"),fileMap.get("video.m4s"));
        }
    }

}
